#ifndef _SESSION
#define _SESSION
#include <uv.h>
#include <memory>
#include "Buf.h"
class Session;	
typedef std::shared_ptr<Session> SessionPtr;
typedef std::function<void(SessionPtr session, char* szByte, int len)> SessionReadCb;
typedef std::function<void(SessionPtr session)> SessionCloseCb;
class Session :public std::enable_shared_from_this <Session>
{
public:
	template<class C = Session> static SessionPtr NewSeesion(uv_stream_t*streamm){
		SessionPtr ses(new Session(streamm));
		//ses->_thisPtr = ses;
		return ses;
	}
	virtual ~Session();
	void OnRead(SessionReadCb sReadcb);
	void OnClose(SessionCloseCb cb){ _sClosecb = cb; }
	void CloseSession();
	void Write(char* buf, size_t bsize);	
	void SetFlag(bool bClient){ m_bClient = bClient; }
private:	
	static void _OnReadCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf);
	static void _OnWriteCb(uv_write_t* req, int status);
	static void _OnCloseCb(uv_handle_t* handle);
	static void _AllocBuf(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
	Session(uv_stream_t*streamm);
public:
	SBuf _rbuf;
	unsigned long long id;
	//SessionPtr _thisPtr=nullptr;
	SessionReadCb _sReadcb = nullptr;
	SessionCloseCb _sClosecb = nullptr;
	bool m_bClient = false;
private:	
	uv_stream_t*_streamm=nullptr;			
};
#endif

